package com.example.administrator.text;

import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.JumpActivityUtils;
import util.LogUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;

/**
 * 地图
 */
public class MapActivity extends CheckPermissionsActivity {
    //经纬度
    private  double latitude;
    private  double longitude;
    private Bundle mBundle;
    //private MapView mMapView = null;
    private Logger mLogger = Logger.getLogger(MapActivity.class);
    private TextView mTVAddewss;
    private String mType = "";
    private String mId,mToken;
   /* *//**
     * 声明AMapLocationClient类对象
     *//*
    public AMapLocationClient mLocationClient = null;
    *//**
     * 声明AMapLocationClientOption对象
     *//*
    public AMapLocationClientOption mLocationOption = null;
    *//**
     * 声明定位回调监听器
     *//*
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    LogUtil.eE("","onLocationChanged....a...."+aMapLocation);
                    String addewss = aMapLocation.getAddress();      //位置
                    latitude = aMapLocation.getLatitude();    //  获取纬度
                    longitude = aMapLocation.getLongitude();  //  获取经度
                    if(mTVAddewss != null){mTVAddewss.setText(addewss);}
                    LogUtil.eE("","addewss........"+addewss+"....latitude.."+latitude+"..longitude.."+longitude);
                }else {
                    if(mTVAddewss != null){mTVAddewss.setText("获取位置信息失败");}
                }
            }

        }
    };

    private AMap mAMap;*/

    @Bind(R.id.activity_map_fl)    FrameLayout mFrameLoayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mBundle = savedInstanceState;
        init(savedInstanceState);
    }

    private void init(Bundle savedInstanceState){
        mTVAddewss = (TextView)findViewById(R.id.activity_map_addewss);
        mId = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mUseridDB, "");
        mToken = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mTokenDB, "");
        HomeUtil.getHemeUtilNew().getStatusBar(this,mFrameLoayout);
        ButterKnife.bind(this);
        mType = getIntent().getStringExtra("type");
        /**获取地图控件引用*/
       /* mMapView = (MapView) findViewById(R.id.activity_map);
        *//**创建地图*//*
        mMapView.onCreate(savedInstanceState);
        if (mAMap == null) {
            LogUtil.eE("","......................................1");
            mAMap = mMapView.getMap();
            setUpMap();
            UiSettings uiSettings = mAMap.getUiSettings();
            uiSettings.setZoomControlsEnabled(false);//隐藏缩放按钮
        }else {
            LogUtil.eE("","......................................2");
        }
        setLatLng();
        mAMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                LogUtil.eE("","...................onMapLoaded...................2");
                LatLng marker1 = new LatLng(latitude, longitude);
                //设置中心点和缩放比例
                mAMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1));
                mAMap.moveCamera(CameraUpdateFactory.zoomTo(17));
                if(latitude !=0 && longitude !=0){
                    initData();
                    HomeUtil.getHemeUtilNew().uploadLocation(MapActivity.this,latitude+":"+longitude);
                }
            }
        });*/
    }

    /**
     * 我要上网
     */
   @OnClick(R.id.activity_map_networking)
   void networking(){
       if(mType !=null){
           if(mType.equals("wifi")){
               finish();
           }else {
               finish();
               JumpActivityUtils.getInstance().jumpHome(this,"map");
           }
       }
   }

    /**
     * 刷新
     */
    @OnClick(R.id.activity_map_refresh)
    void refresh(){
       // mAMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude)));
    }

    /**
     * 扫一扫
     */
    @OnClick(R.id.activity_map_iv_scan)
    void mapCan(){JumpActivityUtils.getInstance().jumpZXing(this);}

    /**
     * 疑问
     */
    @OnClick(R.id.activity_map_doubt)
    void doubt(){
        JumpActivityUtils.getInstance().jumpSetNetFiItem(MapActivity.this);
    }

    /**
     * 设置标记
     */
    private void setLatLng(){
       /* ArrayList<MarkerOptions> markerOptionses = new ArrayList<>();
        LatLng latLng = new LatLng(22.530005,113.931221);
        LatLng latLng1 = new LatLng(22.527151,113.93069);
        LatLng latLng2 = new LatLng(22.528009,113.933828);
        LatLng latLng3 = new LatLng(22.528573,113.933013);
        LatLng latLng4 = new LatLng(22.528395,113.934901);
        LatLng latLng5 = new LatLng(22.529624,113.932734);
        LatLng latLng6 = new LatLng(22.531041,113.931843);
        LatLng latLng7 = new LatLng(22.530684,113.929869);
        LatLng latLng9 = new LatLng(22.531982,113.933817);
        LatLng latLng8 = new LatLng(22.531556,113.929773);
        LatLng latLng10 = new LatLng(22.528157,113.930856);
        LatLng latLng11 = new LatLng(22.528127,113.931382);
        LatLng latLng12 = new LatLng(22.528336,113.931843);
        LatLng latLng13 = new LatLng(22.529455,113.929655);
        LatLng latLng14 = new LatLng(22.531953,113.930985);
        LatLng latLng15 = new LatLng(22.531804,113.932873);
        markerOptionses.add(new MarkerOptions().position(latLng).title("深大").snippet("牛郎网络").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng2).title("深大").snippet("海志楼").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng3).title("深大").snippet("砖石楼").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng4).title("深大").snippet("海寿楼").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng5).title("深大").snippet("留学生楼").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng6).title("深大").snippet("生命科学院").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng7).title("深大").snippet("西海明珠花园").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng8).title("深大").snippet("友邦公寓").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng9).title("深大").snippet("化学与环境工程学院").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng10).title("深大").snippet("食堂").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng11).title("深大").snippet("食堂").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng12).title("深大").snippet("食堂").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng13).title("深大").snippet("西海明珠花园").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng14).title("深大").snippet("西海明珠花园").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        markerOptionses.add(new MarkerOptions().position(latLng15).title("深大").snippet("西海明珠花园").icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.mipmap.location))));
        mAMap.addMarkers(markerOptionses,true);*/
    }


    /**
     *  设置一些amap的属性
     *  LOCATION_TYPE_SHOW  取消随时到定位位置
     *  其他三个都是设置 连续定位的
     */
    private void setUpMap() {
       /* MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类
        myLocationStyle.strokeColor(Color.argb(0,0,0,0));       //设置定位蓝点精度圆圈的边框颜色的方法
        myLocationStyle.radiusFillColor(Color.argb(0,0,0,0)); //设置定位蓝点精度圆圈的填充颜色的方法。
        //myLocationStyle.strokeWidth(5f);                       //设置定位蓝点精度圈的边框宽度的方法。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_SHOW);
        mAMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //设置返回地址信息，默认为true
        mAMap.getUiSettings().setMyLocationButtonEnabled(false);//设置默认定位按钮是否显示，非必需设置。
        mAMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
       //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setWifiScan(true);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        *//**
         * 定位模式
         * Battery_Saving 低功耗定位模式：不会使用GPS和其他传感器，只会使用网络定位（Wi-Fi和基站定位）；
         * Hight_Accuracy 会同时使用网络定位和GPS定位，优先返回最高精度的定位结果，
         *//*
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        mLocationOption.setInterval(5000);
        mLocationOption.setWifiActiveScan(false); //设置是否强制刷新WIFI，默认为true，强制刷新。
        //打开缓存机制
        mLocationOption.setLocationCacheEnable(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
       //启动定位
        mLocationClient.startLocation();*/

    }

    /**
     * 加载数据
     */
    private void initData(){
        HashMap mMap = new HashMap<>();
        mMap.put("longitude",latitude);
        mMap.put("latitude",longitude);
        mMap.put("span",1001);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.getUrlNew().showMap,stringJson,mToken,mId,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId,Url.getUrlNew().showMap,stirngSign,time);
        LogUtil.eE("", "areaid" + MyApplication.getApp().getSiteAreaid() + "token" +mToken + "userid" + mId);
        mLogger.info("initData*()---->fwq--" +MyApplication.mFWQIP + "stringJson--" + stringJson+"...NF_callid..."+map1.get("NF_callid"));
        GreenTreeNetworkUtil.getInstance().doPost(this,MyApplication.mFWQIP,1,map1, new RequestDataCallback() {
            @Override
            public void onSuccess(String response) {
                mLogger.info("initData*()---->onSuccess--" +response);
            }

            @Override
            public void onFailure(Throwable error) {
                StringUtil.getStringUtilNew().getExceptionCode(MapActivity.this, error.toString());
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
      /*  mMapView.onDestroy();
        mLocationClient.onDestroy();*/
        LogUtil.eE("",".....................................onDestroy.1");
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        //mMapView.onResume();
        LogUtil.eE("","....................................onResume..1");
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        //mMapView.onPause();
        LogUtil.eE("",".............................onPause.........1");
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        //mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.eE("","onActivityResult......1");
        switch (requestCode) {
            case RESULT_FIRST_USER:
                if (resultCode == RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    //显示扫描到的内容
                    String string = bundle.getString("result");
                    mLogger.debug("onActivityResult---->扫码--->成功" + string);
                    HomeUtil.getHemeUtilNew().getSessionId(this, string, mId, mToken);
                }
                break;
        }
    }

}
